//
//  LKAddEmployeerViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKAddEmployeerViewController.h"


@implementation LKEmployerRoleModel

+(NSDictionary<NSString *,id> *)modelCustomPropertyMapper{
    return @{@"Id":@"id"};
}
@end


@interface LKAddEmployeerViewController ()
@property(nonatomic,strong)UFFormView *formView;
@property(nonatomic,strong)NSMutableArray *rolesArray;
@property(nonatomic,strong)NSMutableArray *roleModelsArray;
@property(nonatomic,copy)NSString *pwd;
@property(nonatomic,copy)NSString *userPhone;
@property(nonatomic,copy)NSString *roleId;
@property(nonatomic,copy)NSString *userName;
@property(nonatomic,copy)NSString *roleName;
@end

@implementation LKAddEmployeerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf)
    self.navigationItem.title = @"新增员工";
//    [self createFormView];
    [self getRoleDataArray];
    LKBaseButton *bottomButton = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"新增员工" titleColor:[UIColor whiteColor] layercorne:25 font:[UIFont fontWithName:@"PingFangSC-Medium" size:18] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        [SVProgressHUD showWithStatus:@""];
        for (LKEmployerRoleModel *model in weakSelf.roleModelsArray) {
            if ([model.role_name isEqualToString:weakSelf.roleName]) {
                weakSelf.roleId = model.Id;
            }
        }
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"pwd":@"123456",@"role_id":[NSString judegeIsNullAndTransform:weakSelf.roleId],@"user_name":[NSString judegeIsNullAndTransform:weakSelf.userName],@"user_phone":[NSString judegeIsNullAndTransform:weakSelf.userPhone]}];
         NSString *sign = [NSString encoingWithDic:param];
         [param setObject:sign forKey:@"sign"];
         [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyEmoloyerAddUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
             
         } success:^(BOOL isSuccess, id  _Nullable responseObject) {
             [SVProgressHUD dismiss];
             if ([responseObject[@"code"] isEqualToString:@"success"]) {
                 [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"addNewEmployerNotice" object:nil];
                 [weakSelf.navigationController popViewControllerAnimated:YES];
             }else{
                 [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
             }
         } failure:^(NSString * _Nullable errorMessage) {
             
         }];
    }];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(30 + NAVIGATION_BAR_HEIGHT + 208);
        make.height.mas_equalTo(50);
    }];


    // Do any additional setup after loading the view.
}
-(void)createFormView{
    WS(weakSelf)
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])
            .addRow([UFPickerViewRow makePickerViewRow:^(UFPickerViewRowMaker * _Nonnull make) {
                make
                .itemArray(weakSelf.rolesArray)
                .placeholder(@"请选择")
                .title(@"角色")
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.roleName = value;
                })
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"姓名")
                .name(@"user_phone")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.userName = value;
                })
                .value(weakSelf.userName);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"手机号")
                .name(@"pwd")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.userPhone = value;
                })
                .value(weakSelf.userPhone);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"密码")
                .name(@"referrer")
                .value(@"123456(初始登录密码)");
            }]);
        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(208);
    }];
    self.formView.scrollEnabled = NO;
}
-(NSMutableArray *)rolesArray{
    if (_rolesArray == nil) {
        _rolesArray = [[NSMutableArray alloc] init];
    }
    return _rolesArray;
}
-(NSMutableArray *)roleModelsArray{
    if (_roleModelsArray == nil) {
        _roleModelsArray = [[NSMutableArray alloc] init];
    }
    return _roleModelsArray;
}
-(void)getRoleDataArray{
    [SVProgressHUD showWithStatus:@""];
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{}];
     NSString *sign = [NSString encoingWithDic:param];
     [param setObject:sign forKey:@"sign"];
     [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyEmployerRoleUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
         
     } success:^(BOOL isSuccess, id  _Nullable responseObject) {
         [SVProgressHUD dismiss];
         if ([responseObject[@"code"] isEqualToString:@"success"]) {
             NSArray *dataArray = responseObject[@"data"];
             for (NSDictionary *dic in dataArray) {
                LKEmployerRoleModel *model = [LKEmployerRoleModel modelWithDictionary:dic];
                 [self.rolesArray addObject:model.role_name];
                 [self.roleModelsArray addObject:model];
                 [self createFormView];
             }
         }else{
             [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
         }
     } failure:^(NSString * _Nullable errorMessage) {
         
     }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
